﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.JQuery.VSDoc.js" assembly="Dimok.Web.UI" />
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI" />
/// <reference path="../../Manager/Admin/Admin.asmx" />
/// <reference path="../../JS/UI/Dimok.Table.js" />
Type.registerNamespace("motobankcorp");
motobankcorp.Loans = function(element) {
  motobankcorp.Loans.initializeBase(this, [element]);
  Dimok.AccessorizeType(this);
  this.set_carPriceMode(Dimok.CarPriceModes.prototype.Current);
}
motobankcorp.Loans.prototype = {
  initialize: function() {
    motobankcorp.Loans.callBaseMethod(this, 'initialize');
    Sys.Application.add_load(Function.createDelegate(this, function() {
      this.get_RGE().add_rowContextMenuItemClicked(Function.createDelegate(this, this._onRowContextMenuItemClicked));
      this.get_RGE().add_rowContextMenuShowing(Function.createDelegate(this, this._onRowContextMenuShowing));
    }));
  },
  dispose: function() {
    motobankcorp.Loans.callBaseMethod(this, 'dispose');
  },
  _getCreditMethod: function(loanID, bureauID, onSuccess, onError) {
    return Dimok.FindWSMethod("GetCredit", WSA)(loanID, bureauID, onSuccess, onError);
  },
  _callCarPriceWebMethod: function(keys, bookValueTypeID, onCompleteDelegate) {
    return Dimok.FindWSMethod(this.get_carPriceMode(), WSA)(this.get_wsEntityType(), keys.get_LoanID(), bookValueTypeID, onCompleteDelegate, onCompleteDelegate);
  },
  _getCredit: function(bureauID, isBatch) {
    if (isBatch === undefined) { this._showLoadPanel(); isBatch = this.get_batchKeys().length > 1; }
    var keys = Dimok.$A(this.get_batchKeys().shift());
    var gridDataItem = this.findGridDataItem(keys) || undefined;
    if (!keys) return this._hideLoadPanel();
    var selfDelegate = Function.createDelegate(this, function() { this._getCredit(bureauID, isBatch); });
    var batchTitle = isBatch ? "Credit " : "";
    var onCompleteDelegate = Function.createDelegate(this, function(result) {
      if (result && !result.get_message)
        $J.bubbleTip({ pos: { top: 30, left: 3 }, html: result[0], stay: true, cols: 80 });
      this._onComplete(result, keys, batchTitle, gridDataItem, selfDelegate);
    });
    this._getCreditMethod(keys.get_LoanID(), bureauID, onCompleteDelegate, onCompleteDelegate);
  },
  _onRowContextMenuItemClicked: function(sender, eventArgs) {
    eventArgs.set_cancel(true);
    var loanID = eventArgs.get_GridDataItem().get_dataItem().LoanID;
    switch (eventArgs._item.get_value()) {
      case "GetCredit":
        if (!this.set_batchKeys(true).length || !confirm("Get credit for " + _loans(this.get_batchKeys()) + "?")) return this.set_batchKeys();
        return this._getCredit(parseInt(eventArgs.get_item().get_attributes().getAttribute("TypeID")));
      default:
        eventArgs.set_cancel(false); break;
    }
    function _loans(batchKeys) {
      return batchKeys.length == 1 ? Sys.Serialization.JavaScriptSerializer.serialize(batchKeys[0]) + " loan" : batchKeys.length + " loans";
    }
  },
  _onRowContextMenuShowing: function(sender, eventArgs) {
    var text = sender.get_grid().get_selectedItems().length ? "Selected" : "ALL!!!";
    var mi = eventArgs.get_contextMenu()._findItemByValue("Credit");
    if (!mi) $J.bubbleTip({ element: this.get_RGE().get_element(), html: "Unable to find GetCarPrice menu." });
    mi.set_text("Pull Credit for " + text);
  }
}
motobankcorp.Loans.registerClass('motobankcorp.Loans', Dimok.Table);
